package cn.bxshey.modules_order.service.impl;

import cn.bxshey.feign.api.UserClient;
import cn.bxshey.general.Result.Result;
import cn.bxshey.general.entity.Order;
import cn.bxshey.general.entity.User;
import cn.bxshey.modules_order.mapper.OrderMapper;
import cn.bxshey.modules_order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @作者：不想说话而已
 * @Date：2023/11/27 22:29
 */
@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    private final OrderMapper orderMapper;
    private final UserClient userClient;
    @Autowired
    public OrderServiceImpl(OrderMapper orderMapper,
                     UserClient userClient){
        this.orderMapper=orderMapper;
        this.userClient=userClient;
    }

    //根据用户ID获取订单信息
    @Override
    public List<Order> selectByUserId(Long id) {
        return orderMapper.selectByUserId(id);
    }

    //获取所有订单信息
    @Override
    public List<Order> selectGetAll() {
        System.err.println(orderMapper.selectGetAll());
        return orderMapper.selectGetAll();
    }

    // 根据订单id，分页获取用户所有订单的信息
    @Override
    public Order selectById(Long id) {
        Order order = orderMapper.selectById(id);
        Result<User> userResult = userClient.selectById(order.getOrderId());
         if(userResult.getCode().equals(200)){
             order.setUser(userResult.getData());
         }else {
             throw new RuntimeException();
         }
        return order;
    }
}
